package org.fullhappy.dao;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import org.fullhappy.entity.Gallery;

public class GalleryDAO extends DAO<Gallery> {
	private static final Logger LOGGER = Logger
			.getLogger("org.fullhappy.dao.GalleryDAO");

	public GalleryDAO() {
		super(Gallery.class);
	}

	@Override
	public void delete(Gallery gallery) throws Exception {
		EntityManager entityManager = EMF.get().createEntityManager();
		EntityTransaction txn = entityManager.getTransaction();
		gallery = entityManager.find(Gallery.class, gallery.getId());
		if (gallery != null) {
			txn.begin();
			try {
				entityManager.remove(gallery);
				txn.commit();
			} catch (Exception e) {
				LOGGER.log(Level.OFF, "Delete Exception " + e.getMessage());
				throw e;
			} finally {
				if (txn.isActive()) {
					txn.rollback();
				}
				entityManager.close();
			}
		}
	}

}
